package com.xuhuan.thread.study2.example;

/**
 * @author huan.xu
 * @date 2021/9/28 19:25
 * @description 买火车票的例子
 */
public class ThreadTicket implements Runnable {

    /**
     * 票数
     */
    private int ticketNums = 10;

    @Override
    public void run() {
        while (true) {
            if (ticketNums <= 0) {
                break;
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "--->拿到了第" + ticketNums-- + "张票");
        }
    }

    public static void main(String[] args) {
        ThreadTicket ticket=new ThreadTicket();
        new Thread(ticket,"线程1").start();
        new Thread(ticket,"线程2").start();
        new Thread(ticket,"线程3").start();
    }
}
